原生js中添加定时消失的提示框 您所在的位置:网站首页 layui 消息提醒 原生js中添加定时消失的提示框

原生js中添加定时消失的提示框

2023-11-08 21:28| 来源: 网络整理| 查看: 265

方法一:推荐使用 弹层组件文档 - layui.layer,他家的弹窗类似于自定义内容,上手也很容易,js和css引入,直接粘贴添加自己的内容即可用,例如自定义弹窗:

代码:

layer.open({ title: '提示' ,content: 'hello' });

一段时间后自动消失且不需要任何操作的提示框:

代码:

layer.msg('hello');

官网给的示例代码:

//信息框-例1 layer.alert('见到你真的很高兴', {icon: 6}); //信息框-例2 layer.msg('你确定你很帅么?', { time: 0 //不自动关闭 ,btn: ['必须啊', '丑到爆'] ,yes: function(index){ layer.close(index); layer.msg('雅蠛蝶 O.o', { icon: 6 ,btn: ['嗷','嗷','嗷'] }); } }); //信息框-例3 layer.msg('这是最常用的吧'); //信息框-例4 layer.msg('不开心。。', {icon: 5}); //信息框-例5 layer.msg('玩命卖萌中', function(){ //关闭后的操作 }); //页面层-自定义 layer.open({ type: 1, title: false, closeBtn: 0, shadeClose: true, skin: 'yourclass', content: '自定义HTML内容' }); //页面层-图片 layer.open({ type: 1, title: false, closeBtn: 0, area: ['auto'], skin: 'layui-layer-nobg', //没有背景色 shadeClose: true, content: $('#tong') }); //iframe层-父子操作 layer.open({ type: 2, area: ['700px', '450px'], fixed: false, //不固定 maxmin: true, content: 'test/iframe.html' }); //iframe层-多媒体 layer.open({ type: 2, title: false, area: ['630px', '360px'], shade: 0.8, closeBtn: 0, shadeClose: true, content: '//player.youku.com/embed/XMjY3MzgzODg0' }); layer.msg('点击任意处关闭'); //iframe层-禁滚动条 layer.open({ type: 2, area: ['360px', '500px'], skin: 'layui-layer-rim', //加上边框 content: ['mobile/', 'no'] }); //加载层-默认风格 layer.load(); //此处演示关闭 setTimeout(function(){ layer.closeAll('loading'); }, 2000); //加载层-风格2 layer.load(1); //此处演示关闭 setTimeout(function(){ layer.closeAll('loading'); }, 2000); //加载层-风格3 layer.load(2); //此处演示关闭 setTimeout(function(){ layer.closeAll('loading'); }, 2000); //加载层-风格4 layer.msg('加载中', { icon: 16 ,shade: 0.01 }); //打酱油 layer.msg('尼玛,打个酱油', {icon: 4}); //tips层-上 layer.tips('上', '#id或者.class', { tips: [1, '#0FA6D8'] //还可配置颜色 }); //tips层-右 layer.tips('默认就是向右的', '#id或者.class'); //tips层-下 layer.tips('下', '#id或者.class', { tips: 3 }); //tips层-左 layer.tips('左边么么哒', '#id或者.class', { tips: [4, '#78BA32'] }); //tips层-不销毁之前的 layer.tips('不销毁之前的', '#id或者.class', { tipsMore: true }); //默认prompt layer.prompt(function(val, index){ layer.msg('得到了'+val); layer.close(index); }); //屏蔽浏览器滚动条 layer.open({ content: '浏览器滚动条已锁', scrollbar: false }); //弹出即全屏 var index = layer.open({ type: 2, content: 'http://layim.layui.com', area: ['320px', '195px'], maxmin: true }); layer.full(index); //正上方 layer.msg('灵活运用offset', { offset: 't', anim: 6 }); //更多例子 layer.msg('Hi');

这里,面应该有你需要的那一款。

方法二:自定义一段时间后自己消失的提示框:

Document btn // 创建一个div var lunbo=document.createElement("div"); // 设置div的id值 lunbo.id="lunbo"; // 上面按钮点击事件 function func(date){ /* 创建div的样式,宽200px,高80px,下面的是css样式居中, * css样式居中具体了解链接:https://blog.csdn.net/A_Bear/article/details/80546181 */ var style={ background:"#f00", position:"absolute", zIndex:10, width:"200px", height:"80px", left:"50%", top:"50%", marginLeft:"-100px", marginTop:"-40px", color:"white" } for(var i in style) lunbo.style[i]=style[i]; // 当找不到id为lunbo的控件时 if(document.getElementById("lunbo")==null){ // 在body中添加lunbo控件(lunbo在上面创建的) document.body.appendChild(lunbo); // 显示内容 lunbo.innerHTML=date; // 文本居中 lunbo.style.textAlign="center"; lunbo.style.lineHeight="80px"; // 作用是调节字体行高与div同高,使其保持水平居中 // 设置2s后去掉弹出窗 setTimeout("document.body.removeChild(lunbo)",2000) } }

效果:

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有